set
R  region/
$include ..\define/region17.set
WLD
*OECD90,REF,MAF,LAM
*Industrial,Transition,Developing
/
R_AgMIP region in agmip/
$include ..\define/region_agmip.set
/
MAPR_AgMIP(R,R_AgMIP)/
$include ..\define/region_agmip.map
/
Y/2010,2020,2030,2040,2050/
Y_future(Y)/2020,2030,2040,2050/
SSP/SSP2,SSP3,SSP2_TRD1,SSP2_TRD2,SSP2_TRD3/
Sgcm/hg2e,ge2m,ipsl,mes_,ne1m,nocc/
Srcp/RCP26,RCP85,nocc/
co2fert/co2,noco2,nocc/
SCENARIO/
$offlisting
$include ..\define/map/allscenario.set
SSP2_BaU_NoCC
SSP2_TRD1_BaU_NoCC
SSP2_TRD3_BaU_NoCC
SSP3_BaU_NoCC
SSP2_BaU_ge2m_RCP26_co2
SSP2_BaU_ge2m_RCP26_noco2
SSP2_BaU_ge2m_RCP85_co2
SSP2_BaU_ge2m_RCP85_noco2
SSP2_BaU_hg2e_RCP26_co2
SSP2_BaU_hg2e_RCP26_noco2
SSP2_BaU_hg2e_RCP85_co2
SSP2_BaU_hg2e_RCP85_noco2
SSP2_BaU_ipsl_RCP26_co2
SSP2_BaU_ipsl_RCP26_noco2
SSP2_BaU_ipsl_RCP85_co2
SSP2_BaU_ipsl_RCP85_noco2
SSP2_BaU_mes__RCP26_co2
SSP2_BaU_mes__RCP26_noco2
SSP2_BaU_mes__RCP85_co2
SSP2_BaU_mes__RCP85_noco2
SSP2_BaU_ne1m_RCP26_co2
SSP2_BaU_ne1m_RCP26_noco2
SSP2_BaU_ne1m_RCP85_co2
SSP2_BaU_ne1m_RCP85_noco2
$ontext
$include ..\define/scenario2.set
$include ..\define/scenario3.set
$offtext

$include ..\define/map/2030sens.set
$include ..\define/map/2040sens.set
$include ..\define/map/2050sens.set
$include ..\define/map/2050SSP3.set

$onlisting
/
SYR /2010*2060,nocc/
N /N0*N100/
N_out(N)/
N0*N100
*N0*N10
/
C_AGMIP/
WHT,CGR,RIC,OSD,
CR5,AGR,PAS,CRP,ALL
/
Title_Agmip/XPRP,AREA,PROD,YILD,YEXO,CALO,POPU,POP,
VCAL	manetary amount of food consumption
/
Title_Agmip_agg(Title_Agmip) Flag for  aggregated variable from 17 to agmip regions /XPRP,AREA,PROD,YILD,YEXO,POP,VCAL/
Value/dum/
U        fao based or economic model based /fao_based/

WEIGHTMAP(Title_Agmip,Title_Agmip)/
	XPRP    .       PROD
	YILD    .       AREA
	YEXO    .       AREA
/
;
alias(Y,Y2),(Title_Agmip,Title_Agmip1);


*---Longterm
set
MAP_scneario_longterm(SCENARIO,SSP,Sgcm,Srcp,co2fert)/
SSP2_BaU_ge2m_RCP26_co2	.	SSP2	.	ge2m	.	RCP26	.	co2
SSP2_BaU_ge2m_RCP26_noco2	.	SSP2	.	ge2m	.	RCP26	.	noco2
SSP2_BaU_ge2m_RCP85_co2	.	SSP2	.	ge2m	.	RCP85	.	co2
SSP2_BaU_ge2m_RCP85_noco2	.	SSP2	.	ge2m	.	RCP85	.	noco2
SSP2_BaU_hg2e_RCP26_co2	.	SSP2	.	hg2e	.	RCP26	.	co2
SSP2_BaU_hg2e_RCP26_noco2	.	SSP2	.	hg2e	.	RCP26	.	noco2
SSP2_BaU_hg2e_RCP85_co2	.	SSP2	.	hg2e	.	RCP85	.	co2
SSP2_BaU_hg2e_RCP85_noco2	.	SSP2	.	hg2e	.	RCP85	.	noco2
SSP2_BaU_ipsl_RCP26_co2	.	SSP2	.	ipsl	.	RCP26	.	co2
SSP2_BaU_ipsl_RCP26_noco2	.	SSP2	.	ipsl	.	RCP26	.	noco2
SSP2_BaU_ipsl_RCP85_co2	.	SSP2	.	ipsl	.	RCP85	.	co2
SSP2_BaU_ipsl_RCP85_noco2	.	SSP2	.	ipsl	.	RCP85	.	noco2
SSP2_BaU_mes__RCP26_co2	.	SSP2	.	mes_	.	RCP26	.	co2
SSP2_BaU_mes__RCP26_noco2	.	SSP2	.	mes_	.	RCP26	.	noco2
SSP2_BaU_mes__RCP85_co2	.	SSP2	.	mes_	.	RCP85	.	co2
SSP2_BaU_mes__RCP85_noco2	.	SSP2	.	mes_	.	RCP85	.	noco2
SSP2_BaU_ne1m_RCP26_co2	.	SSP2	.	ne1m	.	RCP26	.	co2
SSP2_BaU_ne1m_RCP26_noco2	.	SSP2	.	ne1m	.	RCP26	.	noco2
SSP2_BaU_ne1m_RCP85_co2	.	SSP2	.	ne1m	.	RCP85	.	co2
SSP2_BaU_ne1m_RCP85_noco2	.	SSP2	.	ne1m	.	RCP85	.	noco2
SSP2_BaU_NoCC	.	SSP2	.	nocc	.	nocc	.	nocc
SSP3_BaU_NoCC	.	SSP3	.	nocc	.	nocc	.	nocc
SSP2_TRD1_BaU_NoCC	.	SSP2_TRD1	.	nocc	.	nocc	.	nocc
SSP2_TRD3_BaU_NoCC	.	SSP2_TRD3	.	nocc	.	nocc	.	nocc
/
;
parameter
Pagmipout_longterm_load(SCENARIO,Y,R,C_AGMIP,Title_Agmip,Value)
Pagmipout_longterm_load2(SCENARIO,Y,R,C_AGMIP,Title_Agmip,Value)
Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip)
Pagmipout_longterm1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip)

Risk_agg_longterm(U,SCENARIO,R_agmip,Y)
PCAL_agg_longterm(U,SCENARIO,R_agmip,Y)
;

$gdxin '../data/longterm/final_results.gdx'
$load Pagmipout_longterm_load=Pagmipout_exe
$gdxin '../data/longterm/final_results_sens.gdx'
$load Pagmipout_longterm_load2=Pagmipout_exe

$gdxin '../data/longterm/AIMCGE_longterm.gdx'
$load Risk_agg_longterm=Risk_agg
$load PCAL_agg_longterm=PCAL_agg


Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip)$(Title_Agmip_agg(Title_Agmip) and SUM(SCENARIO$(MAP_scneario_longterm(SCENARIO,SSP,Sgcm,Srcp,co2fert)),Pagmipout_longterm_load(SCENARIO,Y,R,C_AGMIP,Title_Agmip,"dum")))
=SUM(SCENARIO$(MAP_scneario_longterm(SCENARIO,SSP,Sgcm,Srcp,co2fert)),Pagmipout_longterm_load(SCENARIO,Y,R,C_AGMIP,Title_Agmip,"dum"));
Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip)$(Title_Agmip_agg(Title_Agmip) and SUM(SCENARIO$(MAP_scneario_longterm(SCENARIO,SSP,Sgcm,Srcp,co2fert)),Pagmipout_longterm_load2(SCENARIO,Y,R,C_AGMIP,Title_Agmip,"dum")))
=SUM(SCENARIO$(MAP_scneario_longterm(SCENARIO,SSP,Sgcm,Srcp,co2fert)),Pagmipout_longterm_load2(SCENARIO,Y,R,C_AGMIP,Title_Agmip,"dum"));

Pagmipout_longterm1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,"AGR","POPU")$(SUM(SCENARIO$(MAP_scneario_longterm(SCENARIO,SSP,Sgcm,Srcp,co2fert)),Risk_agg_longterm("fao_based",SCENARIO,R_agmip,Y)))
=SUM(SCENARIO$(MAP_scneario_longterm(SCENARIO,SSP,Sgcm,Srcp,co2fert)),Risk_agg_longterm("fao_based",SCENARIO,R_agmip,Y));
Pagmipout_longterm1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,"AGR","CALO")$(SUM(SCENARIO$(MAP_scneario_longterm(SCENARIO,SSP,Sgcm,Srcp,co2fert)),PCAL_agg_longterm("fao_based",SCENARIO,R_agmip,Y)))
=SUM(SCENARIO$(MAP_scneario_longterm(SCENARIO,SSP,Sgcm,Srcp,co2fert)),PCAL_agg_longterm("fao_based",SCENARIO,R_agmip,Y));


*---Longterm end

*--- Food category coverage for the paper
set
C_AGMIP_targeted(C_AGMIP)/WHT,CGR,RIC,OSD/;
;

parameter
Foodcatratio(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert)
;

Foodcatratio(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert)$Pagmipout_longterm1(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,"AGR","CALO")
=SUM(C_AGMIP$C_AGMIP_targeted(C_AGMIP),Pagmipout_longterm1(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,C_AGMIP,"CALO"))/Pagmipout_longterm1(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,"AGR","CALO");

Execute_unload '..\data\Foodcatratio.gdx'
Foodcatratio
Pagmipout_longterm1
;

$exit

parameter
	Pagmipout(Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)
	Pagmipout1(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)
        Pagmipout_diff(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)
;


$batinclude loadinggdx 202050
$batinclude loadinggdx 2030
$batinclude loadinggdx 2040
$batinclude loadinggdx 2050co2
$batinclude loadinggdx 2030sens
$batinclude loadinggdx 2040sens
$batinclude loadinggdx 2050sens
$batinclude loadinggdx 2050SSP3

$ontext
$batinclude loadinggdx 2020SSP3
$batinclude loadinggdx 2030SSP3
$batinclude loadinggdx 2040SSP3

$batinclude loadinggdx 2050SSP3co2
$offtext

*---Regional aggregation from 17 to agmip regions for variable other than POPU and CALO

Pagmipout1(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)$(Title_Agmip_agg(Title_Agmip) and SUM((R,Title_agmip1)$(MAPR_AgMIP(R,R_AgMIP) AND WEIGHTMAP(Title_Agmip,Title_Agmip1)),Pagmipout(Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip1)))
=SUM(R$(MAPR_AgMIP(R,R_AgMIP)),SUM(Title_agmip1$(WEIGHTMAP(Title_Agmip,Title_Agmip1)),Pagmipout(Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip1))*Pagmipout(Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip))
/SUM((R,Title_agmip1)$(MAPR_AgMIP(R,R_AgMIP) AND WEIGHTMAP(Title_Agmip,Title_Agmip1)),Pagmipout(Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip1));
Pagmipout1(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)$(Title_Agmip_agg(Title_Agmip) and SUM((R,Title_agmip1)$(MAPR_AgMIP(R,R_AgMIP) AND WEIGHTMAP(Title_Agmip,Title_Agmip1)),Pagmipout(Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip1))=0)
=SUM(R$(MAPR_AgMIP(R,R_AgMIP)),Pagmipout(Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip));

Pagmipout_longterm1(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip)$(Title_Agmip_agg(Title_Agmip) and SUM((R,Title_agmip1)$(MAPR_AgMIP(R,R_AgMIP) AND WEIGHTMAP(Title_Agmip,Title_Agmip1)),Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip1)))
=SUM(R$(MAPR_AgMIP(R,R_AgMIP)),SUM(Title_agmip1$(WEIGHTMAP(Title_Agmip,Title_Agmip1)),Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip1))*Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip))
/SUM((R,Title_agmip1)$(MAPR_AgMIP(R,R_AgMIP) AND WEIGHTMAP(Title_Agmip,Title_Agmip1)),Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip1));
Pagmipout_longterm1(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip)$(Title_Agmip_agg(Title_Agmip) and SUM((R,Title_agmip1)$(MAPR_AgMIP(R,R_AgMIP) AND WEIGHTMAP(Title_Agmip,Title_Agmip1)),Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip1))=0)
=SUM(R$(MAPR_AgMIP(R,R_AgMIP)),Pagmipout_longterm(Y,R,SSP,Sgcm,Srcp,co2fert,C_AGMIP,Title_Agmip));


*---FAO
parameter
PCAL_fao(R_agmip,Y)
Risk_fao(R_agmip,Y)
;
$gdxin '../data/Risk_fao.gdx'
$load PCAL_fao=PCAL_fao_tot Risk_fao



Pagmipout1("2010",R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,"AGR","CALO")$(PCAL_fao(R_agmip,"2010") and SUM(Y2,Pagmipout1(Y2,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,"AGR","CALO")))=PCAL_fao(R_agmip,"2010");
Pagmipout_longterm1("2010",R_agmip,SSP,Sgcm,Srcp,co2fert,"AGR","CALO")$(PCAL_fao(R_agmip,"2010") and sum(Y2,Pagmipout_longterm1(Y2,R_agmip,SSP,Sgcm,Srcp,co2fert,"AGR","CALO")))=PCAL_fao(R_agmip,"2010");

Pagmipout1("2010",R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,"AGR","POPU")$(PCAL_fao(R_agmip,"2010") and SUM(Y2,Pagmipout1(Y2,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,"AGR","POPU")))=Risk_fao(R_agmip,"2010")*1000;
Pagmipout_longterm1("2010",R_agmip,SSP,Sgcm,Srcp,co2fert,"AGR","POPU")$(PCAL_fao(R_agmip,"2010") and sum(Y2,Pagmipout_longterm1(Y2,R_agmip,SSP,Sgcm,Srcp,co2fert,"AGR","POPU")))=Risk_fao(R_agmip,"2010")*1000;

*---FAO end

Pagmipout_diff(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)$(Pagmipout1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip) and Pagmipout_longterm1(Y,R_agmip,SSP,"nocc","nocc","nocc",C_AGMIP,Title_Agmip))
=Pagmipout1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)-Pagmipout_longterm1(Y,R_agmip,SSP,"nocc","nocc","nocc",C_AGMIP,Title_Agmip);


$ontext
*---VCAL [USD/cap/day] --> This value is not used in the analysis. VCAL has been calcuated in FoodStock.gms using the nocc level of price.
set
Ausda/
"Wheat"
"Coarse graings"
"Corn"
"Rice"
"Cereal"
/
;
parameter
        kcal_g(Ausda)	conversion factor from gramme to kcal [kcal per gramme]
	g_kcal
;

kcal_g("Wheat")=535/183;
kcal_g("Coarse graings")=7/2.63;
kcal_g("Corn")=143/47.61;
kcal_g("Rice")=711/287.08;
kcal_g("Cereal")=(kcal_g("Wheat")+kcal_g("Rice")+kcal_g("Corn"))/3;
g_kcal=1/kcal_g("Cereal");

* USD/cap/day = kcal/cap/day * g_cerealeq/kcal * tonne/g * USD/t
Pagmipout1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,"AGR","VCAL")=Pagmipout1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,"AGR","CALO")*g_kcal*10**(-6)*Pagmipout1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,"CR5","XPRP");
Pagmipout_longterm1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,"AGR","VCAL")=Pagmipout_longterm1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,"AGR","CALO")*g_kcal*10**(-6)*Pagmipout_longterm1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,"CR5","XPRP");
$offtext

$ontext
*---N100 --> N30
parameter
Pagmipout2(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)
Pagmipout_diff1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)
;

Pagmipout2(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)$(N_out(N))=Pagmipout1(Y,R_AgMIP,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip);
Pagmipout_diff1(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip)$(N_out(N))=Pagmipout_diff(Y,R_agmip,SSP,Sgcm,Srcp,co2fert,SYR,N,C_AGMIP,Title_Agmip);
$offtext




Execute_unload '..\data\Pagmipout_rev.gdx'
Pagmipout1=Pagmipout
Pagmipout_diff=Pagmipout_diff
Pagmipout_longterm1=Pagmipout_longterm
;

$exit




















*Data_nocc(var,Y,R,SSP,A)$(Data(var,Y,R,SSP,"nocc","nocc","nocc","nocc","N0",A))=Data(var,Y,R,SSP,"nocc","nocc","nocc","nocc","N0",A);
*Data(var,Y,R,SSP,"nocc","nocc","nocc","nocc","N0",A)=0;


set
Ragg/
US
LAM
EU
China
SAsia
SEAsia
Afr_ME
FSU
Other
WLD
/
MAP_Ragg(R,Ragg)/
USA	.	US
XE25	.	EU
XER	.	EU
TUR	.	Other
XOC	.	Other
CHN	.	China
IND	.	SAsia
JPN	.	Other
XSE	.	SEAsia
XSA	.	SAsia
CAN	.	Other
BRA	.	LAM
XLM	.	LAM
CIS	.	FSU
XME	.	Afr_ME
XNF	.	Afr_ME
XAF	.	Afr_ME
USA	.	WLD
XE25	.	WLD
XER	.	WLD
TUR	.	WLD
XOC	.	WLD
CHN	.	WLD
IND	.	WLD
JPN	.	WLD
XSE	.	WLD
XSA	.	WLD
CAN	.	WLD
BRA	.	WLD
XLM	.	WLD
CIS	.	WLD
XME	.	WLD
XNF	.	WLD
XAF	.	WLD
/
;

set
var/
PPOP_UNSH
PCAL
YEXO
XPRP
AREA
PROD
CONS
YILD
NETT
FOOD
FEED
OTH

FAREA
FYILD
FNETT
FFEED
FOTH

dCONS
dFOOD
error
error_ratio
/
var1(var)/
PPOP_UNSH
PCAL
YEXO
XPRP
/
var2(var)/
AREA
PROD
CONS
YILD
YEXO
/
var_decomp(var)/
dFOOD
FAREA
FYILD
FNETT
FFEED
FOTH
error
/
A       /PDR,WHT,GRO,OSD,dum,CALL,CP4/
ACP4(A) /PDR,WHT,GRO,OSD/
;

parameter
	Data(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)
        Data_nocc(var,Y,R,SSP,A)
        Data_decomp(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)


        Data_agg(var,Y,Ragg,SSP,Sgcm,Srcp,co2fert,SYR,N,A)
        Data_nocc_agg(var,Y,Ragg,SSP,A)
;

        Data_agg(var,Y,Ragg,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(sameas(var,"PPOP_UNSH"))=sum(R$MAP_Ragg(R,Ragg),Data(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A));
        Data_nocc_agg(var,Y,Ragg,SSP,A)$(sameas(var,"PPOP_UNSH"))=sum(R$MAP_Ragg(R,Ragg),Data_nocc(var,Y,R,SSP,A));

Execute_unload '..\data\Dataall.gdx'
Data,Data_nocc,PPOP_UNSH
Data_agg,Data_nocc_agg
;

$exit

$setglobal YTGT 2050


Data_decomp("dCONS",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(set_plot(Y,SYR,N) and CONS("SSP2_BaU_NoCC","%YTGT%",R,A) AND SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),CONS(SCENARIO,Y,R,A)))
=SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),CONS(SCENARIO,Y,R,A))-CONS("SSP2_BaU_NoCC","%YTGT%",R,A);
Data_decomp("dFOOD",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(set_plot(Y,SYR,N) and FOOD("SSP2_BaU_NoCC","%YTGT%",R,A) AND SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),FOOD(SCENARIO,Y,R,A)))
=SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),FOOD(SCENARIO,Y,R,A))-FOOD("SSP2_BaU_NoCC","%YTGT%",R,A);

Data_decomp("FAREA",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(set_plot(Y,SYR,N) and AREA("SSP2_BaU_NoCC","%YTGT%",R,A) AND SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),AREA(SCENARIO,Y,R,A)) and YILD("SSP2_BaU_NoCC","%YTGT%",R,A) AND SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),YILD(SCENARIO,Y,R,A)))
=(SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),AREA(SCENARIO,Y,R,A))-AREA("SSP2_BaU_NoCC","%YTGT%",R,A))*YILD("SSP2_BaU_NoCC","%YTGT%",R,A)
+(1/2)*((SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),AREA(SCENARIO,Y,R,A))-AREA("SSP2_BaU_NoCC","%YTGT%",R,A))*(SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),YILD(SCENARIO,Y,R,A))-YILD("SSP2_BaU_NoCC","%YTGT%",R,A)))
;
Data_decomp("FYILD",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(set_plot(Y,SYR,N) and AREA("SSP2_BaU_NoCC","%YTGT%",R,A) AND SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),AREA(SCENARIO,Y,R,A)) and YILD("SSP2_BaU_NoCC","%YTGT%",R,A) AND SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),YILD(SCENARIO,Y,R,A)))
=(SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),YILD(SCENARIO,Y,R,A))-YILD("SSP2_BaU_NoCC","%YTGT%",R,A))*AREA("SSP2_BaU_NoCC","%YTGT%",R,A)
+(1/2)*((SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),AREA(SCENARIO,Y,R,A))-AREA("SSP2_BaU_NoCC","%YTGT%",R,A))*(SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),YILD(SCENARIO,Y,R,A))-YILD("SSP2_BaU_NoCC","%YTGT%",R,A)))
;
Data_decomp("FNETT",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(set_plot(Y,SYR,N) and NETT("SSP2_BaU_NoCC","%YTGT%",R,A) AND SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),NETT(SCENARIO,Y,R,A)))
=(-1)*(SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),NETT(SCENARIO,Y,R,A))-NETT("SSP2_BaU_NoCC","%YTGT%",R,A));
Data_decomp("FFEED",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(set_plot(Y,SYR,N) and FEED("SSP2_BaU_NoCC","%YTGT%",R,A) AND SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),FEED(SCENARIO,Y,R,A)))
=(-1)*(SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),FEED(SCENARIO,Y,R,A))-FEED("SSP2_BaU_NoCC","%YTGT%",R,A));
Data_decomp("FOTH",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(set_plot(Y,SYR,N) and OTH("SSP2_BaU_NoCC","%YTGT%",R,A) AND SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),OTH(SCENARIO,Y,R,A)))
=(-1)*(SUM(SCENARIO$MAP_scneario(SCENARIO,SSP,Sgcm,Srcp,co2fert,SYR,N),OTH(SCENARIO,Y,R,A))-OTH("SSP2_BaU_NoCC","%YTGT%",R,A));

Data_decomp(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,"CP4")=SUM(A$ACP4(A),Data_decomp(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A));

Data_decomp(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(POP("SSP2_BaU_NoCC",Y,R))=Data_decomp(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)/(POP("SSP2_BaU_NoCC",Y,R)*10**3)/365.24;

Data_decomp("Error",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)=Data_decomp("dFOOD",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)-(
Data_decomp("FAREA",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)
+Data_decomp("FYILD",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)
+Data_decomp("FNETT",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)
+Data_decomp("FFEED",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)
+Data_decomp("FOTH",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)
);

Data_decomp("Error_ratio",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(Data_decomp("dFOOD",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A))=Data_decomp("Error",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)/Data_decomp("dFOOD",Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A);


*Data_plot(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(rcp_plot(Srcp) AND ssp_plot(ssp))=Data(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A);
file resultsall3 /..\data\Data_decomp_%YTGT%.txt/;
resultsall3.pc=6;
put resultsall3;
loop((var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)$(var_decomp(var) and set_plot(Y,SYR,N) and Data_decomp(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A)),
     put var.tl,Y.tl,R.tl,SSP.tl,Sgcm.tl,Srcp.tl,co2fert.tl,SYR.tl,N.tl,A.tl,Data_decomp(var,Y,R,SSP,Sgcm,Srcp,co2fert,SYR,N,A):10:5/
);

Execute_unload '..\data\Data_decomp_%YTGT%.gdx'
Data_decomp
;

